#include <stdio.h>
//#include <stdbool.h>

#include <stdint.h>
#define SIH688X_REG_CHIP_ID					0x87
#define HAPTIC_CONFIG_MAX_REG_NUM					256
#define sih6887_F0_VAL_MAX							2500
#define sih6887_F0_VAL_STANDARD						2350
#define sih6887_F0_VAL_MIN							2200
#define sih6887_F0_CAL_COE							100000
#define sih6887_F0_CALI_DELTA						288
#define sih6887_TRACKING_F0_COE						1966080000
#define sih6887_GET_VBAT_MAX_TRY					10
#define sih6887_ADC_AMPLIFY_COE						1000
#define sih6887_ADC_COE								320
#define sih6887_VBAT_MIN							3000
#define sih6887_STANDARD_VBAT						4200
#define sih6887_VBAT_MAX							5500
#define sih6887_MAX_GAIN							255
#define sih6887_PROTECTION_TIME						30000
#define sih6887_WAIT_FOR_STANDBY_MAX_TRY			200

#define SIH_I2C_OPERA_BYTE_ONE                      1
#define SIH_I2C_OPERA_BYTE_TWO                      2
#define SIH_I2C_OPERA_BYTE_THREE                    3
#define SIH_I2C_OPERA_BYTE_FOUR                     4
#define SIH_I2C_OPERA_BYTE_FIVE                     5
#define SIH_I2C_OPERA_BYTE_SIX                      6
#define SIH_I2C_OPERA_BYTE_SEVEN                    7
#define SIH_I2C_OPERA_BYTE_EIGHT                    8
#define SIH_RESET_GPIO_HIGH							1
#define SIH_RESET_GPIO_LOW							0
#define SIH_F0_PRE_VALUE							2350

#define SIH_F0_DETECT_TRY							4

#define LOG_FLAG  "haptic_driver"

#define LOG_LEVEL_INFO        4
#define LOG_LEVEL_WARNING     3
#define LOG_LEVEL_DEBUG       2
#define LOG_LEVEL_ERROR       1

#define LOG_LEVEL      4
#define hp_printf(log_level,format, ...) \
  			printf("[%s][%s][%s][%s][%d] "format,log_level,LOG_FLAG, __FILE__,__func__, __LINE__, ##__VA_ARGS__)

#define hp_dbg(format, ...) \
do{ \
            if(LOG_LEVEL>=LOG_LEVEL_DEBUG){  \
                hp_printf("debug",format, ##__VA_ARGS__); \
            } \
}while(0)


#define hp_info(format, ...) \
do{ \
            if(LOG_LEVEL>=LOG_LEVEL_INFO){  \
                hp_printf("info",format, ##__VA_ARGS__); \
            } \
}while(0)

#define hp_err(format, ...) \
do{ \
            if(LOG_LEVEL>=LOG_LEVEL_ERROR){  \
                hp_printf("err",format,  ##__VA_ARGS__); \
            } \
}while(0)
            
#define hp_warn(format, ...) \
do{ \
            if(LOG_LEVEL>=LOG_LEVEL_WARNING){  \
                hp_printf("warning",format,  ##__VA_ARGS__); \
            } \
}while(0)

#define haptic_i2c_read(reg, len, data)  IIC_mem_read_bytes(reg, data, len)
#define haptic_i2c_write(reg, len, data) IIC_mem_write_bytes(reg, data, len)

typedef struct haptic_reg_format {
	uint8_t reg_addr;
	uint8_t reg_value;
} haptic_bin_file_reg_format_t;

typedef struct haptic_reg_config {
	uint8_t reg_num;
	haptic_bin_file_reg_format_t reg_cont[HAPTIC_CONFIG_MAX_REG_NUM];
} haptic_reg_config_t;

typedef enum motorParam_type {
	SIH_RAM_LOOP_MODE = 0,
	SIH_RAM_MODE =1,
	SIH_SET_GAIN = 2,
	SIH_OTHER = 3,

} motorParam_type_e;

typedef enum sih_haptic_cali_lra {
	SIH_WRITE_ZERO = 0,
	SIH_F0_CALI_LRA = 1,
} sih_haptic_cali_lra_e;


typedef struct {
	uint8_t wave;
	uint8_t loop;
} WaveParam;

typedef struct {
	motorParam_type_e type;
	WaveParam waveParam;
	uint8_t gain;
} MotorParam;

// typedef struct haptic_regmap {
// 	struct regmap *regmapping;
// 	const struct regmap_config *config;
// } haptic_regmap_t;

// typedef struct sih_ram_para {
// 	int index;
// 	uint32_t base_addr;
// 	uint8_t wave_num;				/* ram library's wave num */
// 	uint8_t ram_vbat_comp;
// } sih_ram_para_t;

// typedef struct sih_detect_para {
// 	uint8_t f0_cali_data;
// 	uint32_t detect_f0;
// 	uint32_t tracking_f0;
// 	uint32_t vbat;
// 	uint32_t cali_target_value;
// } sih_detect_para_t;

// typedef struct sih_chip_inter_para {
// 	uint8_t gain;
// 	uint32_t interval_us;
// } sih_chip_inter_para_t;

// typedef struct sih_chip_attr {
// 	int32_t reset_gpio;
// 	int32_t irq_gpio;
// } sih_chip_attr_t;

// typedef struct sih_haptic {
// 	sih_chip_attr_t chip_attr;
// 	sih_chip_inter_para_t chip_ipara;
// 	sih_detect_para_t detect;
// 	sih_ram_para_t ram;

// } sih_haptic_t;

uint8_t sih6887_read_chipid(void);
int32_t sih6887_check_chipid(uint8_t chip_id);
int32_t sih6887_init();//初始化
int32_t sih6887_HapticStart();//开始播放
int32_t sih6887_HapticStop();//停止播放
int32_t sih6887_HapticCalib();//校准
int32_t sih6887_SetParam(MotorParam *motorParam);//设置参数
int32_t haptic_msleep(uint32_t ms);
void sih6887_dump_reg(void);

int haptic2_init();
